﻿// Decompiled with JetBrains decompiler
// Type: System.Web.Razor.Generator.ExpressionCodeGenerator
// Assembly: System.Web.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
// MVID: 682513D6-F37E-48AC-9909-F3EE103CFF14
// Assembly location: D:\Project\Idea\无人机外网\uav\客户端工程\排查工具\bin\Release\System.Web.Razor.dll

using System.Linq;
using System.Web.Razor.Parser.SyntaxTree;

namespace System.Web.Razor.Generator
{
  /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Represents a code generator for expression.</summary>
  public class ExpressionCodeGenerator : HybridCodeGenerator
  {
    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Generates the start code for the block.</summary>
    /// <param name="target">The target block the start code generation.</param>
    /// <param name="context">The code generator context.</param>
    public override void GenerateStartBlockCode(Block target, CodeGeneratorContext context)
    {
      if (context.Host.EnableInstrumentation && context.ExpressionRenderingMode == ExpressionRenderingMode.WriteToOutput)
      {
        Span contentSpan = target.Children.OfType<Span>().Where<Span>((Func<Span, bool>) (s => s.Kind == SpanKind.Code || s.Kind == SpanKind.Markup)).FirstOrDefault<Span>();
        if (contentSpan != null)
          context.AddContextCall(contentSpan, context.Host.GeneratedClassContext.BeginContextMethodName, false);
      }
      string fragment = context.BuildCodeString((Action<CodeWriter>) (cw =>
      {
        if (context.Host.DesignTimeMode)
        {
          context.EnsureExpressionHelperVariable();
          cw.WriteStartAssignment("__o");
        }
        else
        {
          if (context.ExpressionRenderingMode != ExpressionRenderingMode.WriteToOutput)
            return;
          if (!string.IsNullOrEmpty(context.TargetWriterName))
          {
            cw.WriteStartMethodInvoke(context.Host.GeneratedClassContext.WriteToMethodName);
            cw.WriteSnippet(context.TargetWriterName);
            cw.WriteParameterSeparator();
          }
          else
            cw.WriteStartMethodInvoke(context.Host.GeneratedClassContext.WriteMethodName);
        }
      }));
      context.BufferStatementFragment(fragment);
      context.MarkStartOfGeneratedCode();
    }

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Generates the end code for the block.</summary>
    /// <param name="target">The target block for the end code generation.</param>
    /// <param name="context">The code generator context.</param>
    public override void GenerateEndBlockCode(Block target, CodeGeneratorContext context)
    {
      string fragment = context.BuildCodeString((Action<CodeWriter>) (cw =>
      {
        if (context.ExpressionRenderingMode == ExpressionRenderingMode.WriteToOutput)
        {
          if (!context.Host.DesignTimeMode)
            cw.WriteEndMethodInvoke();
          cw.WriteEndStatement();
        }
        else
          cw.WriteLineContinuation();
      }));
      context.MarkEndOfGeneratedCode();
      context.BufferStatementFragment(fragment);
      context.FlushBufferedStatement();
      if (!context.Host.EnableInstrumentation || context.ExpressionRenderingMode != ExpressionRenderingMode.WriteToOutput)
        return;
      Span contentSpan = target.Children.OfType<Span>().Where<Span>((Func<Span, bool>) (s => s.Kind == SpanKind.Code || s.Kind == SpanKind.Markup)).FirstOrDefault<Span>();
      if (contentSpan == null)
        return;
      context.AddContextCall(contentSpan, context.Host.GeneratedClassContext.EndContextMethodName, false);
    }

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Generates code for the expression.</summary>
    /// <param name="target">The source span whose content represents an expression.</param>
    /// <param name="context">The code generator context.</param>
    public override void GenerateCode(Span target, CodeGeneratorContext context)
    {
      Span sourceSpan = (Span) null;
      if (context.CreateCodeWriter().SupportsMidStatementLinePragmas || context.ExpressionRenderingMode == ExpressionRenderingMode.WriteToOutput)
        sourceSpan = target;
      context.BufferStatementFragment(target.Content, sourceSpan);
    }

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Returns the string representation of this instance.</summary>
    /// <returns>The string representation of this instance.</returns>
    public override string ToString() => "Expr";

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Indicates whether this instance and a specified object are equal.</summary>
    /// <returns>true if <paramref name="obj" /> and this instance are the same type and represent the same value; otherwise, false.</returns>
    /// <param name="obj">The object to compare with the current instance.</param>
    public override bool Equals(object obj) => obj is ExpressionCodeGenerator;

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Returns the hash code for this instance.</summary>
    /// <returns>The hash code for this instance.</returns>
    public override int GetHashCode() => base.GetHashCode();
  }
}
